#include "RingQueue.hpp"
#include "Task.hpp"


void* productor_routine(void* args){
    RingQueue<Task>* ringqueue = static_cast<RingQueue<Task>*>(args);

    while(true){
        //sleep(2);
        int x = rand() % 10;
        int y = rand() % 10;
        int op = rand() % optionlist.size();
        Task t(x,y,optionlist[op],mymath);
        ringqueue->push(t);
        std::cout<<"生产者生产数据:"<<t.tostring()<<std::endl;
    }
    return nullptr;
}

void* consumer_routine(void* args){
    RingQueue<Task>* ringqueue = static_cast<RingQueue<Task>*>(args);

    while(true){
        sleep(1);
        Task t;
        ringqueue->pop(&t);
        std::cout<<"消费者消费数据:"<<t()<<std::endl;
    }
    return nullptr;
}





int main(){
    pthread_t p1,p2;
    pthread_t c1,c2;
    RingQueue<Task>* rq = new RingQueue<Task>;
    srand((unsigned int) time(nullptr) ^ getpid() ^ pthread_self());

    pthread_create(&p1,nullptr,productor_routine,rq);
    pthread_create(&p2,nullptr,productor_routine,rq);
    pthread_create(&c1,nullptr,consumer_routine,rq);
    pthread_create(&c2,nullptr,consumer_routine,rq);

    pthread_join(p1,nullptr);
    pthread_join(p2,nullptr);
    pthread_join(c1,nullptr);
    pthread_join(c2,nullptr);
    return 0;
}